package me.xingyannian.zhaopin.service;

import me.xingyannian.zhaopin.entity.ChangCi;
import me.xingyannian.zhaopin.repository.ChangCiRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author xingyannian
 */
@Service
public class ChangCiService {
    private final ChangCiRepository changCiRepository;

    public ChangCiService(ChangCiRepository changCiRepository) {
        this.changCiRepository = changCiRepository;
    }
    // 假删除
    public void delete(Long id) {
        var changCi = changCiRepository.findById(id);
        // 判断结果是否存在
        if (changCi.isEmpty()) {
            return;
        }
        //逻辑删除
        changCi.get().setIsDeleted(true);
        // 保存
        changCiRepository.save(changCi.get());
    }
    // 恢复删除
    public void recover(Long id) {
        var changCi = changCiRepository.findById(id);
        // 判断结果是否存在
        if (changCi.isEmpty()) {
            return;
        }
        //逻辑删除
        changCi.get().setIsDeleted(false);
        // 保存
        changCiRepository.save(changCi.get());
    }
    // 真删除
    public void realDelete(Long id) {
        var changCi = changCiRepository.findById(id);
        // 判断结果是否存在
        if (changCi.isEmpty()) {
            return;
        }
        // 真删除
        changCiRepository.delete(changCi.get());
    }
    // 保存
    public void save(ChangCi changCi) {
        changCiRepository.save(changCi);
    }
    // 查询
    public Optional<ChangCi> findById(Long id) {
        return changCiRepository.findById(id);
    }
    // 查询所有
    public Iterable<ChangCi> findAll() {
        return changCiRepository.findAll();
    }
    // 查询所有未删除
    public Iterable<ChangCi> findAllNotDeleted() {
        return changCiRepository.findAllByIsDeleted(false);
    }
    // 查询所有已删除
    public Iterable<ChangCi> findAllDeleted() {
        return changCiRepository.findAllByIsDeleted(true);
    }
    // 分页查询是否被删除
    public Page<ChangCi> findAllByIsDeletedAndPage(boolean isDeleted, int page, int size) {
        return changCiRepository.findAllByIsDeleted(isDeleted, PageRequest.of(page, size));
    }
    // 分页查询
    public Iterable<ChangCi> findAllByPage(int page, int size) {
        return changCiRepository.findAll(PageRequest.of(page, size));
    }
    //findByIsDeletedFalseOrderByIdDesc
    public Page<ChangCi> findByIsDeletedFalseOrderByIdDesc(int page, int size) {
        return changCiRepository.findByIsDeletedFalseOrderByIdDesc(PageRequest.of(page, size));
    }


}
